package com.wy.business.market.message.actions;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.tools.ant.taskdefs.Concat;
import org.springframework.beans.factory.annotation.Autowired;

import com.itextpdf.text.pdf.PdfStructTreeController.returnType;
import com.wy.business.market.message.services.ContactsService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;

/**
 * 项目名称：openportal
 * 类名称：ContactsAction
 * 类描述：
 * 创建人：田松林
 * 创建时间：2016-1-8 下午2:56:34
 * 修改人：田松林
 * 修改时间：2016-1-8 下午2:56:34
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Action(value="/message/contacts")
public class ContactsAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;
	
	private List<Map> rows;
	
	@Autowired
	private ContactsService contactsService;
	
	@Action(value = "list", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String queryContacts() throws ErrorSqlException, FileNotFoundException, IOException{
//		String userId=ServletActionContext.getRequest().getParameter("userId");
//		String orgCode=ServletActionContext.getRequest().getParameter("orgCode");
		
//		方案一:人员不完全在叶子结点下
//		rows=new ArrayList<Map>();
//		Map<String, Object> map=new HashMap<String, Object>();
//		map.put("ORG", contactsService.queryOrgTree(orgCode, false));
//		map.put("USER", contactsService.queryOrgUsers(orgCode, userId));
//		rows.add(map);
		
//		方案2：人员全部都在叶子节点下
		rows=contactsService.queryOrgTree();
		return SUCCESS;
	}
	
	@Action(value = "users", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String queryOrgUsers() throws ErrorSqlException{
		String userId=ServletActionContext.getRequest().getParameter("userId");
		String orgCode=ServletActionContext.getRequest().getParameter("orgCode");
		String friendUserId=ServletActionContext.getRequest().getParameter("friendUserId");
		rows=contactsService.queryOrgUsers(orgCode, userId,friendUserId);
		return SUCCESS;
	}
	
	@Action(value = "search", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String queryContactSearch() throws ErrorSqlException{
		String userId=ServletActionContext.getRequest().getParameter("userId");
		String searchContent=ServletActionContext.getRequest().getParameter("searchContent");
		rows=new ArrayList<Map>();
		if (!StringUtils.isBlank(searchContent)) {
			rows=contactsService.queryContactSearch(searchContent,userId);
		}
		return SUCCESS;
	}

	public List<Map> getRows() {
		return rows;
	}

	public void setRows(List<Map> rows) {
		this.rows = rows;
	}
	
	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}
	

}
